<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="content">
				<view class="search-box flexWrap">
					<input placeholder="输入手机号/昵称" placeholder-class="placeholderclass"  v-model="keywords"/>
					<view class="search-btn" @tap="search()">
						<image src="../../static/coupon/search-icon.png" class="search-icon"></image>
					</view>
				</view>
				<view class="sort-box flexWrapNo">
					排序
					<view class="sort-item flexWrapNo" @tap="changeType(0)">
						总消费
						<view class="triangle-box flexWarpCenterColumn">
							<text class="triangle up" :class="{active:type==0&&!sortFlag}"></text>
							<text class="triangle" :class="{active:type==0&&sortFlag}"></text>
						</view>
					</view>
					<view class="sort-item flexWrapNo" @tap="changeType(1)">
						余额
						<view class="triangle-box flexWarpCenterColumn">
							<text class="triangle up" :class="{active:type==1&&!sortFlag}"></text>
							<text class="triangle" :class="{active:type==1&&sortFlag}"></text>
						</view>
					</view>
					<view class="sort-item flexWrapNo" @tap="changeType(2)">
						加入时间
						<view class="triangle-box flexWarpCenterColumn">
							<text class="triangle up" :class="{active:type==2&&!sortFlag}"></text>
							<text class="triangle" :class="{active:type==2&&sortFlag}"></text>
						</view>
					</view>
				</view>
				<view class="user-list">
					<view class="user-item" v-for="(item,index) in userList" :key="index">
						<view class="user-box flexWrap">
							<view class="user-info-box flexWrapNo">
								<image :src="item.avatar" class="head-img"></image>
								<view class="user-info">
									<view>{{item.nickname}} <text class="jointime">({{item.jointime}})</text> </view>
									<view>{{item.mobile}} <text style="margin-left: 30upx;color: #50AB6E;" @tap="copy(item.mobile)">复制</text></view>
								</view>
							</view>
							<view class="select-icon" v-if="couponId" @tap="select(index)">
								<image src="../../static/coupon/select-icon.png" v-if="item.check"></image>
								<image src="../../static/order/notselect-icon.png" v-else></image>
							</view>
							<view class="send-btn" @tap="goNext(index)" v-else>发券</view>
						</view>
						<view class="data-box flexWrap">
							<view class="data-item flexWarpCenterColumn blue" @tap="goPage('/pages/manage/memberOrderList?user_id='+item.user_id)">
								<text>￥{{item.total_pay_money}}</text>
								<text class="data-text">总消费</text>
							</view>
							<view class="data-item flexWarpCenterColumn">
								<text>{{item.quantity}}小时</text>
								<text class="data-text">总时长</text>
							</view>
							<view class="data-item flexWarpCenterColumn blue" @tap="goPage('/pages/manage/memberCouponList?user_id='+item.user_id)">
								<text>{{item.coupon}}</text>
								<text class="data-text">优惠券</text>
							</view>
							<view class="data-item flexWarpCenterColumn">
								<text>￥{{item.recharge}}</text>
								<text class="data-text">充值总额</text>
							</view>
							<view class="data-item flexWarpCenterColumn">
								<text>￥{{item.money}}</text>
								<text class="data-text">余额总额</text>
							</view>
						</view>
					</view>
					<uni-load-more v-if="showLoadStatus" :status="status" :size="14" :content-text="contentText" />
					<view class="empty-box" v-if="showEmpty">
						<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
					</view>
				</view>
				<view class="select-box" v-if="selectList.length">
					<view class="select-top flexWrap">
						<view>
							已选<text class="length">{{selectList.length}}</text>人
						</view>
						<view class="send-btn" @tap="goNext()">确定</view>
					</view>
					<view class="select-cont">
						<view class="user-item flexWrapNo" v-for="(item,index) in selectList" :key="index">
							<view class="delete-icon" @tap="deleteUser(index)">
								<image src="../../static/coupon/delete-icon.png" class="delete-icon"></image>
							</view>
							<view class="user-info flexWrapNo">
								<view class="name">{{item.nickname}}</view>
								<view>{{item.mobile}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				pageOpc: 0,
				keywords:"",
				//下拉刷新
				page: 1,
				size:10,
				showEmpty: true,
				showLoadStatus: false,
				more: true,
				contentText: {
					contentdown: '上拉加载更多~',
					contentrefresh: '加载中...',
					contentnomore: '已经到底了~~'
				},
				status: 'loading',
				userList:[],
				selectList:[],
				type:0,
				sortFlag:false,
				couponId:""
			}
		},
		onReady() {
			setTimeout(() => {
				this.pageOpc = 1
			}, 100)
		},
		onLoad(ops) {
			if(ops&&ops.couponId) this.couponId=ops.couponId
			this.getData()
		},
		// 触底刷新
		onReachBottom() {
			if (this.more) {
				this.getData()
			}
		},
		// 下拉刷新
		onPullDownRefresh: function() {
			this.freshList();
			this.$tools.loadIng()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		methods: {
			copy(mobile){
				let that = this
				uni.setClipboardData({
					data:mobile,
					success: function() {
						console.log('success');
					}
				});
			},
			// 刷新列表
			freshList() {
				this.page = 1
				this.more = true
				this.status = 'loading'
				this.userList = []
				this.getData();
			},
			changeType(index){
				let prveIndex = this.type
				this.type=index
				if (index != prveIndex){
					this.sortFlag=false
				}else{
					this.sortFlag=!this.sortFlag
				}
				this.freshList();
			},
			search(){
				if(!this.keywords){
					this.$tools.showToast('请输入手机号')
					return;
				}
				this.freshList();
			},
			getData(){
				let self=this
				this.$request.post(this.$api.searchuser, {
					page:this.page++,
					num:this.size,
					store_id:uni.getStorageSync('store_id'),
					keywords:this.keywords,
					sort_type:Number(this.sortFlag),
					sort:Number(this.type+1)
				}, {
					token:true
				}).then(res => {
					if (res.code == 1) {
						let {
							data = {}
						} = res;
						for (let i in data) {
							data[i].check=false
						}
						let newArray =data
						if (newArray.length == 0 && self.page == 2) {
							self.showEmpty = true
							self.showLoadStatus = false
							self.more = false // 不能再次请求了
							return false;
						} else {
							self.showLoadStatus = true
							self.more = true
							self.showEmpty = false
							self.status = 'loading'
						}
						if (newArray.length < this.size) { // 说明没有更多数据
							self.status = 'noMore'
							self.more = false
							self.showEmpty = false
						}
						self.userList=self.userList.concat(newArray);
					}
				}).finally(() => {
					setTimeout(()=>{
						this.$tools.hideLoading()
					}, 1000)
				})
			},
			select(index){
				this.userList[index].check=!this.userList[index].check
				let item=this.userList[index]
				if(item.check){
					this.selectList.push(item)
				}else{
					this.selectList.splice(this.selectList.indexOf(item),1)
				}
			},
			deleteUser(index){
				let userIndex=this.userList.indexOf(this.selectList[index])
				if(userIndex!=-1) this.userList[userIndex].check=false
				this.selectList.splice(index,1)
			},
			goNext(index){
				if(this.couponId){
					let item=this.userList[index]
					console.log(this.selectList)
					uni.setStorageSync('selectList',this.selectList)
					uni.navigateBack()
				}else{
					uni.navigateTo({
						url:'/pages/manage/sendCoupon?user_id='+this.userList[index].user_id
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 400upx;
	}
	.content {
		padding: 0 36upx;
	}
	.search-box{
		height: 80rpx;
		background: #fff;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		padding: 0 28rpx;
		padding-right: 0;
		.search-btn{
			background: #EEC162;
			border-radius:16upx;
			height: 80upx;
			width: 80upx;
			text-align: center;
			line-height: 90upx;
			position: relative;
			.search-icon{
				width: 40rpx;
				height: 40rpx;
				position: absolute;
				top: 0;
				right: 0;
				bottom: 0;
				left: 0;
				margin: auto;
			}
		}
		input{
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #333;
			flex: 1;
		}
		.placeholderclass{
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #333;
		}
	}
	.sort-box{
		font-size: 24upx;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 60upx;
		color: #333;
		justify-content:flex-end;
		margin-top: 10upx;
		.sort-item{
			margin-left: 25upx;
		}
		.triangle-box{
			.triangle{
				border-top-color: #FFFFFF;
			}
			.up{
				transform: rotate(180deg);
				margin-bottom: 10upx;
			}
			.active{
				border-top-color: #EEC162;
			}
		}
	}
	.user-list {
		margin-top: 36rpx;
		.user-item{
			background: #FFF;
			border-radius: 16rpx;
			padding:32upx 28upx;
			margin-bottom: 30upx;
			.user-box{
				font-size: 24upx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 60upx;
				color: #333;
				.user-info-box{
					.head-img{
						width: 100upx;
						height: 100upx;
						border: 1rpx solid #D0D0D0;
						border-radius: 50%;
						overflow: hidden;
						margin-right: 20upx;
					}
					.jointime{
						margin-left: 10upx;
					}
				}
				.send-btn{
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #333;
					width: 112rpx;
					height: 60rpx;
					line-height: 60rpx;
					text-align: center;
					background: #EEC162;
					border-radius: 16rpx;
				}
				.select-icon {
					width: 60rpx;
					height: 60rpx;
					flex: none;
				}
			}
			.data-box{
				padding-top: 20upx;
				margin-top: 20upx;
				border-top: 1rpx solid #D0D0D0;
				flex-wrap: wrap;
				.data-item{
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #333;
					.data-text{
						font-size: 24rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #6e6e6e;
						margin-top: 10upx;
					}
				}
				.blue{
					color:#EEC162;
				}
			}
		}
	}
	.select-box{
		background:#fff;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		margin: auto;
		z-index: 999;
		.select-top{
			margin:30rpx auto;
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			color: #333;
			padding: 22upx 36upx;
			border-bottom: 1rpx solid #F2F2F2;
			.length{
				color: #EEC162;
				margin: 0 18upx;
			}
			.send-btn{
				font-size: 28rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333;
				width: 112rpx;
				height: 60rpx;
				line-height: 60rpx;
				text-align: center;
				background: #EEC162;
				border-radius: 16rpx;
			}
		}
		.select-cont{
			height: 400rpx;
			overflow-y: scroll;
			.user-item {
				margin-bottom: 38rpx;
				align-items: flex-start;
				padding-left: 36rpx;
				.delete-icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 22rpx;
					flex: none;
				}
				.user-info {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #6E6E6E;
					padding-bottom: 28rpx;
					flex: 1;
					.name {
						margin-right: 36rpx;
					}
				}
			}
		}
	}
</style>
